<template>
  <a-modal 
    :title="title" 
    :width="800" 
    :visible="visible" 
    :confirm-loading="confirmLoading" 
    cancel-text="关闭" 
    @ok="submit" 
    @cancel="closeModal"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="名称">
          <a-input v-decorator="['name', validatorRules.name ]" placeholder="请输入名称" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="会员领取数">
          <a-input-number style="width: 200px" :min="1" placeholder="最低为1" v-decorator="['memberGetCount', validatorRules.memberGetCount ]" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="单用户限领数量">
          <a-input-number style="width: 200px" :min="1" placeholder="最低为1" v-decorator="['limitCount', validatorRules.limitCount ]" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="优惠劵状态">
          <a-switch v-model="status" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="发放数">
          <a-input-number style="width: 200px" :min="1" placeholder="最低为1" v-decorator="[ 'sendCount', {}]" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="使用数">
          <a-input-number style="width: 200px" :min="1" placeholder="最低为1" v-decorator="[ 'useCount', {}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import moment from 'moment'
import ApiTree from '@/api/market/couponList';
import ImageUpload from '@/components/ImageUpload';
import { Slime } from '@/mixins/slime';
export default {
  name: 'couponListModal',
  components: { ImageUpload },
  data() {
    return {
      title: '编辑',
      form: this.$form.createForm(this),
      model: {},
      validatorRules: {
        memberGetCount: { rules: [{ required: true, message: '请输入会员领取数!' }] },
        limitCount: { rules: [{ required: true, message: '请输入单用户限领数量!' }] },
        fullMoney: { rules: [{ required: true, message: '请输入满金额!' }] },
      },
      id: 0,
      //
      merchantIdRadio:0,
      merchantId:0,
      totalRadio:0,
      total:0,
      discountRadio:0,
      daysRadio:0,
      status:false,
      startTime:null,
      endTime:null,
    };
  },
  mixins: [Slime],
  methods: {
    // 增
    crudCreate() {
      this.crudUpdate({});
    },
    // 改
    crudUpdate(record) {
      this.form.resetFields(); //重新设置领域
      this.model = Object.assign({}, record);
      this.visible = true;
      this.id = record.id
      this.$nextTick(() => {
        this.merchantIdRadio = record.merchantId == 0?0:1;
        this.merchantId = record.merchantId
        this.totalRadio = record.total == 0?0:1;
        this.total = record.total
        this.discountRadio = record.discount == 0?0:1;
        this.daysRadio = record.days == 0?1:0;
        this.status = record.status == 0?false:true
        this.form.setFieldsValue({
          name: record.name,
          memberGetCount: record.memberGetCount,
          limitCount: record.limitCount,
          fullMoney: record.fullMoney?record.fullMoney/100:0,
          minusMoney: record.minusMoney?record.minusMoney/100:0,
          discount: record.discount,
          days: record.days,
          startTime: record.startTime,
          endTime: record.endTime,
          applyType: record.applyType,
          sendType: record.sendType,
          sendCount: record.sendCount,
          useCount: record.useCount,
          remark: record.remark,
        });
      });
    },
    // 提交
    submit() {
      const that = this;
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let param = { 
            ...values,
            merchantId: that.merchantIdRadio == 0 ? 0 : that.merchantId,
            total: that.totalRadio == 0 ? 0 : that.total,
            fullMoney: that.numberRound(values.fullMoney),
            minusMoney: that.discountRadio == 0 ? that.numberRound(values.minusMoney) : 0,
            discount: that.discountRadio == 0 ? 0 : values.discount,
            days: that.daysRadio == 0 ? values.days : 0,
            startTime: that.daysRadio == 0 ? null : that.startTime,
            endTime: that.daysRadio == 0 ? null : that.endTime,
            status: that.status?0:1,
          };
          if (!that.id) {
            that.submitCreate(param);
          } else {
            param = {...param, id:that.id}
            that.submitUpdate(param);
          }
        }
      });
    },
    // 提交 增
    submitCreate(param) {
      const that = this;
      ApiTree.getAdd(param)
        .then((res) => {
          that.$message.success('新增成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 提交 改
    submitUpdate(param) {
      const that = this;
      ApiTree.getUpdate(param)
        .then((res) => {
          that.$message.success('编辑成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 弹窗-关闭
    closeModal() {
      this.confirmLoading = false;
      this.visible = false;
      this.form.resetFields();
      this.$emit('closeModal');
    },
    startTimeChange(date){
      let startTime = moment(date).format('YYYY-MM-DD');
      this.startTime = startTime
    },
    endTimeChange(date){
      let endTime = moment(date).format('YYYY-MM-DD');
      this.endTime = endTime
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>